//
//  XTEmoticonTool.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/21.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit

class XTEmoticonTool: NSObject {
    //全局单例
    static let sharedTools:XTEmoticonTool = XTEmoticonTool()
    //构造函数私有化
    private override init() {
        super.init()
    }
    
    private lazy var emoticonBundle:NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        //创建bundle对象
        let bundle = NSBundle(path: path)!
        
        return bundle
    }()
    //读取默认类的表情
    private lazy var defaultEmoticon:[XTEmoticon] = {
       return self.loadEmoticonWith("default/info.plist")
    }()
    
    //读取emoji表情
    private lazy var emojiEmoticon:[XTEmoticon] = {
        return self.loadEmoticonWith("emoji/info.plist")
    
    }()
    
    //读取浪小花表情
    private lazy var lxhEmoticon:[XTEmoticon] = {
        return self.loadEmoticonWith("lxh/info.plist")
    
    }()
    
    //给collectView准备数据
    lazy var allEmoticons:[[[XTEmoticon]]] = {
    
        return [
            
            self.sectionWithEmoticons(self.defaultEmoticon),
            self.sectionWithEmoticons(self.emojiEmoticon),
            self.sectionWithEmoticons(self.lxhEmoticon),
        ]
    }()
    
    private func loadEmoticonWith(subPath:String)->([XTEmoticon]){
    
        //不指定路径时，默认取的是Resources内的文件
        let path = self.emoticonBundle.pathForResource(subPath, ofType: nil)!
        //加载plist数据
        let array = NSArray(contentsOfFile: path)!
        var tempArray = [XTEmoticon]()
        //遍历数组转成模型
        for dic in array{
            let emoticon = XTEmoticon(dic: dic as! [String : AnyObject])
            //判断是否有图片
            if emoticon.type == "0"{
                let png = emoticon.png!
                //删除地址的最后一部分
                let subPath = (path as NSString).stringByDeletingLastPathComponent
                //图片的全路径
                emoticon.path = subPath + "/" + png
            }
            tempArray.append(emoticon)
        }
        return tempArray
    }
    
    //计算页数，截取数据
    private func sectionWithEmoticons(emoticons:[XTEmoticon])->([[XTEmoticon]]){
    
        //计算页数
        let pageCount = (emoticons.count - 1)/20+1
        
        var tempArray :[[XTEmoticon]]  = [[XTEmoticon]]()
        //遍历页书截取索引
        for i in 0..<pageCount{
            //截取的开始位置
            let location = i*20
            //截取的长度
            var length = 20
            
            if location + length > emoticons.count{
                length = emoticons.count - location
            
            }
            
            //截取数组元素
            let subArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(location, length)) as! [XTEmoticon]
            
            tempArray.append(subArray)
        }
    
    
    return tempArray
    }
    
    //通过表情字符串来找到表情模型
    func emoticonWithEmoticonString(emoticonStr:String)->XTEmoticon?{
        
            for value in defaultEmoticon{
                if value.chs == emoticonStr{
                
                return value
            }
        }
        
        for value in lxhEmoticon{
            if value.chs == emoticonStr{
                return value
            }
        }
        return nil
    }
    
}
